package com.hf.lookatme.zview.activitys;

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.hf.lookatme.R;
import com.hf.lookatme.commons.CommonMethod;
import com.hf.lookatme.zbean.LoginRecordData;
import com.hf.lookatme.zpresenter.Presenter_Activity_Login;
import com.hf.lookatme.zview.adapters.Adapter_ListView_LoginRecord;
import com.hf.lookatme.zview.interfaces.IView_Activity_Login;
import com.orhanobut.logger.Logger;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by JoeShiguang on 2015/5/22.
 */
public class Activity_Login extends Activity_Base implements IView_Activity_Login {

  private static String TAG = "Activity_Login";

  private Presenter_Activity_Login presenter;

  @InjectView(R.id.imageView_login_headImg)
  ImageView imageView_headImg;

  @InjectView(R.id.imageView_login_account_icon_loginNameDrop)
  ImageView imageView_loginName_Drop;

  @InjectView(R.id.editText_login_account_loginName)
  EditText editText_loginName;

  @InjectView(R.id.listView_login_account_loginRecords)
  ListView listView_loginRecords;

  @InjectView(R.id.linearLayout_login_account)
  LinearLayout layout_accountInfo;

  @InjectView(R.id.relativeLayout_login_account_loginName)
  RelativeLayout layout_accountInfo_loginName;

  private boolean clickable = true;

  private boolean isShowed_loginRecords = false;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    ButterKnife.inject(this);
    try {

      presenter = new Presenter_Activity_Login(this);

    } catch (Exception ex) {

      Logger.e("onCreate error");
    }
  }

  @OnClick(R.id.textView_login_others_newUser)
  void onTextView_newUserClicked(View view) {
    if (!clickable) {
      return;
    }
    if (CommonMethod.isFastDoubleClick()) {
      return;
    }

    presenter.doIntent(Activity_Login.this, Activity_SignUp.class);
  }

  @OnClick(R.id.textView_login_others_forgetPassword)
  void onTextView_forgetPasswordClicked(View view) {
    if (!clickable) {
      return;
    }
    if (CommonMethod.isFastDoubleClick()) {
      return;
    }

    presenter.doIntent(Activity_Login.this, Activity_ResetPassword.class);
  }

  @OnClick(R.id.button_login_doLogin)
  void onButton_loginClicked(View view) {
    if (!clickable) {
      return;
    }
    if (CommonMethod.isFastDoubleClick()) {
      return;
    }
    presenter.doIntent(Activity_Login.this, Activity_MainPage.class);
  }

  @OnClick(R.id.imageView_login_account_icon_loginNameDrop)
  void onImageView_loginName_DropClicked(View view) {
    if (!clickable) {
      return;
    }
    if (CommonMethod.isFastDoubleClick()) {
      return;
    }
    if (!isShowed_loginRecords) {

      presenter.showPopupWindow_loginRecord(Activity_Login.this);
    } else {

      hideLoginRecords();
    }
  }


  @Override
  public void showPopupWindow_LoginRecord(final List<LoginRecordData> list_loginRecordData) {

    try {
      if (list_loginRecordData != null && list_loginRecordData.size() > 1) {

        ObjectAnimator.ofFloat(imageView_loginName_Drop, "rotationX", 0.0F, 180.0F).setDuration(300).start();
        listView_loginRecords.setVisibility(View.VISIBLE);
        Adapter_ListView_LoginRecord adapter = new Adapter_ListView_LoginRecord(Activity_Login.this, list_loginRecordData);
        listView_loginRecords.setAdapter(adapter);
        int height_item = editText_loginName.getHeight();
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) listView_loginRecords.getLayoutParams();
        if (list_loginRecordData.size() < 3) {
          params.height = height_item * list_loginRecordData.size();
        } else {
          params.height = height_item * 3;
        }
        listView_loginRecords.setLayoutParams(params);
        RelativeLayout.LayoutParams params_Layout_accountInfo = (RelativeLayout.LayoutParams) layout_accountInfo.getLayoutParams();
        params_Layout_accountInfo.height = params_Layout_accountInfo.height + params.height;
        layout_accountInfo.setLayoutParams(params_Layout_accountInfo);
        LinearLayout.LayoutParams params_Layout_LoginName = (LinearLayout.LayoutParams) layout_accountInfo_loginName.getLayoutParams();
        params_Layout_LoginName.height = params_Layout_LoginName.height + params.height;
        layout_accountInfo_loginName.setLayoutParams(params_Layout_LoginName);
        isShowed_loginRecords = true;
        listView_loginRecords.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            editText_loginName.setText(list_loginRecordData.get(i).getLoginName());
            editText_loginName.setSelection(list_loginRecordData.get(i).getLoginName().length());
            hideLoginRecords();
          }
        });
      }
    } catch (Exception ex) {

      Logger.e(ex.getMessage());
    }
  }


  private void hideLoginRecords() {

    try {

      ObjectAnimator.ofFloat(imageView_loginName_Drop, "rotationX", 180.0F, 0.0F).setDuration(300).start();

      int height_added = listView_loginRecords.getHeight();
      RelativeLayout.LayoutParams params_Layout_accountInfo = (RelativeLayout.LayoutParams) layout_accountInfo.getLayoutParams();
      params_Layout_accountInfo.height = params_Layout_accountInfo.height - height_added;
      layout_accountInfo.setLayoutParams(params_Layout_accountInfo);
      LinearLayout.LayoutParams params_Layout_LoginName = (LinearLayout.LayoutParams) layout_accountInfo_loginName.getLayoutParams();
      params_Layout_LoginName.height = params_Layout_LoginName.height - height_added;
      layout_accountInfo_loginName.setLayoutParams(params_Layout_LoginName);
      listView_loginRecords.setVisibility(View.GONE);
      isShowed_loginRecords = false;
    } catch (Exception ex) {
      Logger.e(ex.getMessage());
    }
  }

  @Override
  public boolean dispatchTouchEvent(MotionEvent ev) {

    if (ev.getAction() == MotionEvent.ACTION_DOWN) {

      // 获得当前得到焦点的View，一般情况下就是EditText（特殊情况就是轨迹求或者实体按键会移动焦点）
      View view = getCurrentFocus();
      if (CommonMethod.isShouldHideInput(view, ev)) {
        CommonMethod.hideSoftInput(Activity_Login.this, view.getWindowToken());
      }
    }
    return super.dispatchTouchEvent(ev);
  }


}
